package com.ebt.m.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.Toast;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.thirtylogin.model.ShareListener;
import com.ebt.m.commons.utils.ActivityUtils;
import com.ebt.m.event.ShareEvent;
import com.ebt.m.event.SyncCardEvent;
import com.ebt.m.fileExplore.event.FilePickEvent;
import com.ebt.m.fragment.AuthenticationFragment;
import com.ebt.m.fragment.CompanyFragment;
import com.ebt.m.fragment.WebBaseFragment;
import com.ebt.m.utils.ImageSelectUtil;
import com.tencent.tauth.Tencent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import static com.ebt.m.utils.js.JsObject.REQUEST_CODE_CAMERA;
import static com.ebt.m.utils.js.JsObject.REQUEST_CODE_CAMERA_ID;
import static com.ebt.m.utils.js.JsObject.REQUEST_CODE_CAPTURE;
import static com.ebt.m.utils.js.JsObject.REQUEST_CODE_CAPTURE_ID;

/**
 * 包含webview的activity
 *
 * @author Andy.Wang
 */
public class WebBaseActivity extends BaseToolBarActivity {
    /**
     * 自定义fragment类型，方便包括menu、jsinterface的各自处理
     */
    public static final String WEB_TYPE_FRAGMENT = "web_type_fragment";
    /**
     * 用于显示的title
     */
    public static final String WEB_TYPE_TITLE = "web_type_title";
    /**
     * 用于打开的link
     */
    public static final String WEB_TYPE_LINK = "web_type_link";
    /**
     * 是否隐藏toolbar
     */
    public static final String WEB_TYPE_HIDE_TOOLBAR = "web_type_hide_toolbar";
    /**
     * 是否返回到首页
     */
    public static final String WEB_TYPE_TOP_BACK_TO_MAIN = "topBackToMain";
    public static final int FRAGMENT_4COMPANY = 0;
    public static final int FRAGMENT_4OLD_BASE = 1;
    public static final int FRAGMENT_4NEW_BASE = 2;
    public static final int FRAGMENT_4AUTHENTICATION = 3;

    private OnBackListener mOnBackListenr;
    private boolean isHideToolbar = false;
    public static WebBaseActivity INSTANCE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        INSTANCE = this;
        EventBus.getDefault().register(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey(WEB_TYPE_FRAGMENT) && bundle.containsKey(WEB_TYPE_TITLE)) {
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
            setPageTitle();
            if (bundle.containsKey(WEB_TYPE_HIDE_TOOLBAR)) {
                isHideToolbar = bundle.getBoolean(WEB_TYPE_HIDE_TOOLBAR);
            }
            setDefaultFragment(bundle);
            toolbar.setNavigationOnClickListener(view -> onBackPressed());
        } else {
            Toast.makeText(WebBaseActivity.this, "缺少必要参数", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        INSTANCE = null;
    }

    public void setPageTitle() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            setTitle(bundle.getString(WEB_TYPE_TITLE));
        }
    }

    private void setDefaultFragment(Bundle bundle) {
        if (isHideToolbar) {
            getSupportActionBar().hide();
        }
        WebBaseFragment mContentCurr;

        if (bundle.getInt(WEB_TYPE_FRAGMENT) == FRAGMENT_4COMPANY) {
            mContentCurr = new CompanyFragment();
        } else if (bundle.getInt(WEB_TYPE_FRAGMENT) == FRAGMENT_4OLD_BASE) {
            mContentCurr = new WebBaseFragment();
        } else if (bundle.getInt(WEB_TYPE_FRAGMENT) == FRAGMENT_4AUTHENTICATION) {
            mContentCurr = new AuthenticationFragment();
        } else {
            mContentCurr = new WebBaseFragment();
        }
        if (mContentCurr != null) {
            mContentCurr.setArguments(bundle);
            setOnBackListenr(mContentCurr);
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.content, mContentCurr);
            ft.commit();
        } else {
            Toast.makeText(WebBaseActivity.this, "缺少必要参数", Toast.LENGTH_SHORT).show();
            finish();
        }

    }

    @Override
    public void onBackPressed() {
        if (!mOnBackListenr.onBackPressed()) {
            if (getBaseTitle().equals("名片编辑")) {
                EventBus.getDefault().post(new SyncCardEvent(true, false));
            }
            super.onBackPressed();
        }
    }

    public interface OnBackListener {
        boolean onBackPressed();
    }

    public void setOnBackListenr(OnBackListener onBackListenr) {
        this.mOnBackListenr = onBackListenr;
    }

    private ShareListener mShareListener;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ShareEvent event) {
        if (event != null && event.getShareListener() != null) {
            mShareListener = event.getShareListener();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQUEST_CODE_CAMERA:
            case REQUEST_CODE_CAMERA_ID:
                if (resultCode != RESULT_OK) {
                    return;
                }
                EventBus.getDefault().post(new FilePickEvent(null, FilePickEvent.FromCamera, requestCode));
                break;
            case REQUEST_CODE_CAPTURE:
            case REQUEST_CODE_CAPTURE_ID:
                if (resultCode != RESULT_OK) {
                    return;
                }
                if (data != null) {
                    String path = ImageSelectUtil.getPathFromUri(this, data.getData());
                    if (null != path && !"".equals(path)) {
                        File albumFile = new File(path);
                        if (null != albumFile && albumFile.exists()) {
                            EventBus.getDefault().post(new FilePickEvent(albumFile, FilePickEvent.FromAlbum, requestCode));
                            return;
                        }
                    }
                }
                EventBus.getDefault().post(new FilePickEvent(null, FilePickEvent.None, requestCode));
                break;
            default:
                Tencent.onActivityResultData(requestCode, resultCode, data, mShareListener);
                break;
        }
    }

    /**
     * 显示或者隐藏toobar
     *
     * @param isDisplay
     */
    public void setHideToolbarDisplay(boolean isDisplay) {
        toolbar.setVisibility(isDisplay ? View.VISIBLE : View.GONE);
    }

}
